let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]

// 遍历数组
function traversalArr(arr) {
    if (arr === null) return
    for (let i = 0; i < arr.length; i++) {
        console.log(arr[i]);
    }
}
function recursionArr(arr, i = 0) {
    if (arr === null || i >= arr.length) return
    console.log(arr[i]);
    recursionArr(arr, ++i)

}
// traversalArr(arr)
// recursionArr(arr)

function createNode(value) {
    return {
        value: value,
        next: null
    }
}
function addNodes(root, value=0, count=50) {
    if (value < count) {
        root = {
            value:value,
            next:null
        }
        let node = addNodes(root.next,value+1,count)
        root.next = node
    }else{
        return null
    }
    return root
}

let node = addNodes({},1,50)
let node1 = createNode(1)
let node2 = createNode(2)
let node3 = createNode(3)
let node4 = createNode(4)
let node5 = createNode(5)

node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5

// 循环遍历链表
function traversalLink(root) {
    if (root === null) return
    while (true) {
        if (root.next) {
            root = root.next
        } else {
            break;
        }
    }
}
// 递归遍历，必须有出口
function recursionLink(node) {
    if (node === null) return
    console.log(node.value);
    console.log(node);
    arguments.callee(node.next)
}


function reverseLink(root) {
    if (root == null) return
    let prevNode = null
    let activeNode = root
    let nextNode = root.next
    while (true) {
        // 如果还有下一项
        if (activeNode.next) {
            // 将当前node节点指向上一个节点
            activeNode.next = prevNode
            // 旧节点更新为当前节点
            prevNode = activeNode
            // 当前节点更新为下一个节点
            activeNode = nextNode
            // 下一个节点依次推进
            nextNode = nextNode.next
        } else {
            activeNode.next = prevNode
            break;
        }
    }
    return activeNode
}

// 逆置1
function recursionReverseLink(root, prevNode = null) {
    if (root === null) return
    if (root.next === null) {
        root.next = prevNode
        return root
    } else {
        let nextNode = root.next
        root.next = prevNode
        prevNode = root
        root = nextNode
        return recursionReverseLink(root, prevNode)
    }
}


function recursionReverseLink2(root) {
    if (root.next.next == null) {
        root.next.next = root
        return root.next
    } else {
        let node = recursionReverseLink(root.next)
        root.next.next = root
        root.next = null
        return node
    }
}
function section(root, start, end) {

}
// traversalLink(node1)
// recursionLink(recursionReverseLink(node1))
// let link = recursionReverseLink2(node1)
// recursionLink(link)


// console.time('recursionReverseLink')
// recursionReverseLink(node1)
// console.timeEnd('recursionReverseLink')

function xh(root, i) {
    if (i < 10) {
        console.time('recursionReverseLink')
        let node = recursionReverseLink2(root)
        console.timeEnd('recursionReverseLink')
        xh(node, i + 1)
    }
}
// xh(node1, 0)



function linshi(root){
    if(root ==null)return 
    let prev = null
    while(root.next){
        let next = root.next
        root.next = prev
        prev = root
        root = next        
    }
    return prev
}

function linshi2(root){
    console.log(arguments.callee);
    if(root === null)return
    const reseve = (prev,node)=>{
        if(node.next){
            let next = node.next;
            node.next = prev
            return reseve(node,next)
        }else{
            node.next = prev
            return node
        } 
    }
    return reseve(null,root)
}
// console.log(linshi2(node1));
recursionLink(linshi2(node1))